package com.sthz.facepay.config;

import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * 全局controller层异常捕获
 */
@ControllerAdvice
public class GlobalExceptionHandler {
    @ResponseBody
    @ExceptionHandler(Exception.class)
    public Map<String, Object> exceptionHandler(HttpServletRequest request, HttpServletResponse response, Exception e){
        if (AnnotationUtils.findAnnotation(e.getClass(), ResponseStatus.class)!=null) {
            try {
                throw e;
            } catch (Exception e1) {
                e1.printStackTrace();
            }
        }
        Map<String, Object> resMap=new HashMap<>();
        resMap.put("url", request.getRequestURL());
        resMap.put("class",e.getClass());
        resMap.put("cause",e.getCause());
        resMap.put("message",e.getMessage());
        return resMap;
    }
}
